<template>
  <div>
    <el-form :inline="true" :model="keyword" ref="searchForm" class="demo-form-inline">
      <el-form-item>
        <el-button type="warning" size="small" @click="openAddDialog" icon="el-icon-circle-plus">添加</el-button>
      </el-form-item>
      <el-form-item label="项目编号">
        <el-input v-model="keyword.pmId" placeholder="项目编号"></el-input>
      </el-form-item>
      <el-form-item label="项目名称">
        <el-input v-model="keyword.pmName" placeholder="项目名称"></el-input>
      </el-form-item>
      <el-form-item label="项目经理">
        <el-input v-model="keyword.pmSname" placeholder="项目经理"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="resetSearchForm('searchForm')">重置</el-button>
      </el-form-item>
    </el-form>

<!--添加/更新对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="wmsPm" :rules="wmsPmRules" ref="wmsPmForm">
        <el-form-item label="项目名称" prop="pmName">
          <el-input v-model="wmsPm.pmName" auto-complete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="项目经理" prop="pmSname">
          <el-input v-model="wmsPm.pmSname" auto-complete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="项目成员" prop="pmRname">
          <el-input v-model="wmsPm.pmRname" auto-complete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="劳务单位" prop="unit">
          <el-input v-model="wmsPm.unit" auto-complete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="createTime">
          <el-date-picker
            v-model="wmsPm.createTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="wmsPm.endTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addWmsPm('wmsPmForm')">确 定</el-button>
      </div>
    </el-dialog>

<!--表格区域-->
    <el-table :data="pmList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column prop="pmId" label="项目编号" align="center"/>
      <el-table-column prop="pmName" label="项目名称" align="center"/>
      <el-table-column prop="pmSname" label="项目经理" align="center"/>
      <el-table-column prop="pmRname" label="项目成员" align="center"/>
      <el-table-column prop="unit" label="劳务单位" align="center"/>
      <el-table-column prop="createTime" label="开始时间" align="center"/>
      <el-table-column prop="endTime" label="结束时间" align="center"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import { getList, delById, add, update } from '@/api/pro'
import {title} from "@/settings";
export default {
  data() {
    return {
      loading: true,
      pmList: [],
      pageNum:1,
      pageSize:5,
      currentPage:1,
      total:0,
      keyword: {
        pmId: null,
        pmName: null,
        pmSname: null
      },
      pmIds:[],
      title: '',
      dialogFormVisible: false,
      wmsPm: {
        pmId: null,
        pmName: null,
        pmSname: null,
        pmRname: null,
        unit: null,
        createTime: null,
        endTime: null
      },
      wmsPmRules: {
        pmName: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        pmSname: [
          { required: true, message: '项目经理不能为空', trigger: 'blur' }
        ],
        pmRname: [
          { required: true, message: '项目成员不能为空', trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    title() {
      return title
    },
    fetchData() {
      let params = {
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        pmId:this.keyword.pmId,
        pmName:this.keyword.pmName,
        pmSname:this.keyword.pmSname
      }
      getList(params).then(res => {
        this.pmList = res.data.list
        this.total = res.data.total
        this.pageSize = res.data.pageSize
      })
    },
    // 多选框选中数据
    handleSelectionChange(selectionRowArr) {
      this.pmIds = [];
      for (let i = 0; i < selectionRowArr.length; i++) {
        this.pmIds.push(selectionRowArr[i].pmId)
      }
    },
    // 页面大小改变时触发
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    // 当前页码改变时触发
    handleCurrentChange(val) {
      this.pageNum = val;
      this.fetchData();
    },
    // 模糊搜索
    search() {
      this.fetchData()
    },
    // 重置
    resetSearchForm(formName) {
      this.$refs[formName].resetFields();
      this.keyword={}
      this.fetchData()
    },
    // 根据id删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          pmId:row.pmId
        }
        delById(params).then(res => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.fetchData()
        })
      }).catch(() => {
      })
    },
    // 打开添加弹窗
    openAddDialog(){
      this.title = "添加";
      this.dialogFormVisible = true;
    },
    // 打开编辑弹窗
    handleUpdate(row) {
      this.title = "编辑";
      this.dialogFormVisible = true;
      this.wmsPm = row;
    },
    addWmsPm(formName) {
      this.$refs[formName].validate((valid) => {
        if (this.title === "添加"){
        if (valid) {
          add(this.wmsPm).then(res => {
            this.$message({
              message: '添加成功',
              type: 'success'
            })
            this.fetchData();
            this.dialogFormVisible = false
            this.$refs[formName].resetFields()
          })
        }} else if (this.title === "编辑"){
          if (valid) {
            update(this.wmsPm).then(res => {
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
              this.fetchData();
              this.$refs[formName].resetFields()
              this.dialogFormVisible = false

            })
          }
          console.log('error submit!!')
          return false
        }
      })
    },

  },
  created() {
    this.fetchData()
  },
}
</script>
<style scoped>

</style>
